package com.yuexun.beilunpatient.ui.registration.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.yuexun.beilunpatient.R;
import com.yuexun.beilunpatient.base.BaseEntity;
import com.yuexun.beilunpatient.base.BaseKJActivity;
import com.yuexun.beilunpatient.config.AppConfig;
import com.yuexun.beilunpatient.ui.registration.adapter.RegistrationHosAdapter;
import com.yuexun.beilunpatient.ui.registration.bean.AppointmentHospitalBean;
import com.yuexun.beilunpatient.ui.registration.model.impl.RegistrationModel;
import com.yuexun.beilunpatient.ui.registration.presenter.impl.AppointmentHospitalPresenter;
import com.yuexun.beilunpatient.ui.registration.ui.view.IAppointmentHospitalView;
import com.yuexun.beilunpatient.widget.EmptyLayout;

import org.kymjs.kjframe.ui.ViewInject;
import org.kymjs.kjframe.utils.PreferenceHelper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by chenss on 2018/10/29.
 */
public class Act_Registration_Hospital extends BaseKJActivity implements IAppointmentHospitalView {

    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.content_list)
    ListView contentList;
    @Bind(R.id.empty_layout)
    EmptyLayout emptyLayout;

    ArrayList<AppointmentHospitalBean> hospitalBeans = new ArrayList<>();
    AppointmentHospitalPresenter hospitalPresenter;
    RegistrationHosAdapter adapter;

    @Override
    public void setRootView() {
        setContentView(R.layout.act_registration_hospital);
        ButterKnife.bind(this);
    }

    @Override
    public void initData() {
        hospitalPresenter = new AppointmentHospitalPresenter(new RegistrationModel(), this);
        ApiGetHos();

        adapter = new RegistrationHosAdapter(contentList, hospitalBeans, R.layout.item_registrationhos);
        contentList.setAdapter(adapter);

        emptyLayout.setOnLayoutClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                emptyLayout.setErrorType(EmptyLayout.NETWORK_LOADING);
                ApiGetHos();
            }
        });

        contentList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                AppointmentHospitalBean item = (AppointmentHospitalBean) adapterView.getItemAtPosition(i);
                Bundle bundle = new Bundle();
                bundle.putString("depId", getIntent().getExtras().getString("depId"));
                bundle.putString("hosId", item.getHospital_id());
                bundle.putString("type", "2");//中心科室  科室-医院-医生
                Intent intent = new Intent(aty, Act_Registration_Doctor.class);
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });

        tvTitle.setText(getIntent().getExtras().getString("depName"));
    }

    private void ApiGetHos(){
        Map<String, String> params = new HashMap<>();
        params.put("areaCode", "330206");
        params.put("hospName", "");
        params.put("type", "");
        params.put("sessionUuidPatient", PreferenceHelper.readString(aty, AppConfig.USER_INFO, AppConfig.SESSION_ID));
        hospitalPresenter.appointmenthospitalList(params);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick({R.id.title_back})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.title_back:
                finish();
                break;
        }
    }

    @Override
    public void appointmenthospitalList(BaseEntity<AppointmentHospitalBean> baseEntity) {
        if (baseEntity.getStatus().equals("200")) {
            hospitalBeans = baseEntity.getDatas();
            adapter.refresh(hospitalBeans);

        }else {
            ViewInject.toast(baseEntity.getMessage());
        }

        if (hospitalBeans.size() == 0) {
            emptyLayout.setErrorType(EmptyLayout.NODATA);
        } else {
            emptyLayout.dismiss();
        }
    }
}
